#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/8/24 2:44 下午
@desc:
'''

import pandas as pd
import ujson
import re
from models.audi.audi import CarsPartNumAudiCopy, CarsStructureCollection, CarsPartNumNew
# from models.audi_contrast import CarsSaDesc
from models.audi.audi_sa_code import CarsSaDesc
from models.audi.audi_193 import Vb
from scripts.fenci import remove_tt

audi_parts = CarsPartNumAudiCopy.select().where((CarsPartNumAudiCopy.standard_label == '加速度传感器') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向器油') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向油储液罐') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向油储液罐安装支架') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向油储液罐锁紧盖') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向泵') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向泵安装支架') |
                                                (CarsPartNumAudiCopy.standard_label == '动力转向泵皮带轮') |
                                                (CarsPartNumAudiCopy.standard_label == '动态稳定控制单元') |
                                                (CarsPartNumAudiCopy.standard_label == '动态稳定液压压力传感器') |
                                                (CarsPartNumAudiCopy.standard_label == '动态稳定液压机组') |
                                                (CarsPartNumAudiCopy.standard_label == '动态稳定液压机组安装支架') |
                                                (CarsPartNumAudiCopy.standard_label == '动态稳定液压机组隔热板') |
                                                (CarsPartNumAudiCopy.standard_label == '化妆镜') |
                                                (CarsPartNumAudiCopy.standard_label == '化妆镜灯') |
                                                (CarsPartNumAudiCopy.standard_label == '半轴') |
                                                (CarsPartNumAudiCopy.standard_label == '半轴油封') |
                                                (CarsPartNumAudiCopy.standard_label == '半轴油封防尘盘'))
item = []
flag = 0
for a in audi_parts:
    # 配置代码
    sa_codes = []
    # 发动机排量
    cylinder = []
    # 发动机代码
    vb = []
    # 发动机排量信息
    vb_desc = []

    model_desc = []
    transmission = []
    drive = []
    diff_mech = []
    replace = []
    # 匹配信息
    s = ['2缸', '二缸', '3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
    qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
    bsx = ['7档', '7档自动', '7档手动', '七档', '七档自动', '七档手动', '6档', '6档自动', '6档手动',
           '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动',
           '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动',
           '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
    pl = ['1.6', '1.8', '1.3', '2.0', '2.1', '1.9', '1.7', '2.2', '2.23', '2.3', '1.5', '0.9', '1.1',
          '0.8', '2.8', '2.6', '1.4', '4.2', '3.6', '2.5', '2.4', '3.7', '2.7', '1.2', '3.0', '3.3',
          '0.6', '3.2', '0.4', '5.2', '2.9', '0.1', '0.5', '6.3']
    fdjdm = ['CR', 'CY', 'DS', 'DT', 'DZ', 'EP', 'HP', 'JK', 'JL', 'JN', 'JS', 'JU', 'JV', 'WE', 'WN', 'WT', 'WV', 'WY', 'YP', 'YZ', 'CN', 'DE', 'DR', 'JW', 'KF', 'WB', 'WC', 'WH', 'WU', 'DD', 'HY', 'KE', 'KL', 'KM', 'KV', 'KX', 'NG', 'PV', 'SF', 'SK', 'YN', 'JY', 'KG', 'KH', 'KU', 'MC', 'FY', 'FZ', 'WZ', 'YG', 'YH', 'YK', 'YT', 'YY', 'FS', 'YS', 'ZA', 'ZF', 'XX', 'YJ', 'ZB', 'ZC', 'HA', 'HB', 'HC', 'HD', 'HE', 'HH', 'HJ', 'YV', 'WG', 'WA', 'KJ', 'WJ', 'WK', 'WS', '1Z', 'AAH', 'ABC', 'ABK', 'ACE', 'ACZ', 'ADR', 'AHU', 'ADU', 'KK', 'GV', 'KW', 'MB', 'RR', 'WR', 'WX', 'MG', 'MU', 'SA', 'HX', 'KP', 'KZ', 'PH', 'SL', 'JT', 'NE', 'NF', 'NP', 'PX', 'SH', '3A', '7A', 'NM', 'PM', 'PP', 'PS', 'RA', 'RN', 'RU', 'SB', 'SD', 'SE', 'RS', '6A', 'AAD', '4B', 'NC', 'RT', '1B', '2B', '3B', 'ABH', 'PT', 'ANZ', '1Y', 'ABB', '1T', '3D', 'PR', 'AAZ', 'ABM', 'ABT', 'ADA', 'AAE', 'AAR', 'AAS', 'AAT', 'ABP', 'AAN', 'ABY', 'ADW', 'ACK', 'AEJ', 'AEW', 'AFB', 'AKJ', 'AKN', 'ALG', 'ABZ', 'AEM', 'AGH', 'AHC', 'AKG', 'AKH', 'AEL', 'AEC', 'AFC', 'ADP', 'AEB', 'AFF', 'AFM', 'AFN', 'AFY', 'AHH', 'AHL', 'AHK', 'AEH', 'AGN', 'AGR', 'AGU', 'AHF', 'AJQ', 'AKL', 'ALH', 'APF', 'APG', 'APP', 'APY', 'AQA', 'ARZ', 'ASV', 'ASZ', 'AUL', 'AUM', 'AUQ', 'AVU', 'AMK', 'ARX', 'ARY', 'ATD', 'AXR', 'BAM', 'BFQ', 'CVNA', 'CWNA', 'CWPB', 'DKUA', 'DKUB', 'DKWA', 'DKWB', 'DTAA', 'DTAB', 'AGA', 'AJG', 'AJL', 'AJM', 'ALF', 'AML', 'AMX', 'ANA', 'APR', 'APS', 'APU', 'APZ', 'AQD', 'ARG', 'ARJ', 'ARK', 'ARM', 'AGB', 'APB', 'ALZ', 'AMM', 'ANB', 'APT', 'ATJ', 'ATX', 'AVG', 'AVV', 'AWT', 'AZB', 'APX', 'ATC', 'ASJ', 'AZR', 'ANQ', 'AWL', 'BBG', 'BND', 'AKE', 'ARE', 'BAS', 'BAU', 'BCZ', 'BES', 'AMF', 'ANY', 'ATL', 'AUA', 'BAD', 'BBY', 'BHC', 'ALT', 'AMB', 'ASN', 'AVB', 'AVF', 'AVJ', 'AVK', 'AWA', 'AWX', 'AYM', 'BBJ', 'BBK', 'BDG', 'BDH', 'BDV', 'BEX', 'BFB', 'BFC', 'BKE', 'AKC', 'AQG', 'AKF', 'AQF', 'AQH', 'ARU', 'AUW', 'AVP', 'AYS', 'AZC', 'BCS', 'AGE', 'AJK', 'AJP', 'ALW', 'APC', 'AQE', 'ARH', 'ARN', 'ASM', 'AHA', 'ANK', 'AQJ', 'ARS', 'ASG', 'AZA', 'BFV', 'BHE', 'BPF', 'BVP', 'BVR', 'BCY', 'BRV', 'ASB', 'ASE', 'BFL', 'BFM', 'BHT', 'BMC', 'BNG', 'BPK', 'BSB', 'BSM', 'BTE', 'BVJ', 'BVN', 'BAH', 'AXW', 'AXX', 'AZV', 'BAG', 'BDB', 'BGU', 'BHZ', 'BKC', 'BKD', 'BLF', 'BLP', 'BLR', 'BLS', 'BLX', 'BLY', 'BMB', 'BMJ', 'BMM', 'BMN', 'BPY', 'BSE', 'BSF', 'BUB', 'BUY', 'BVY', 'BVZ', 'BWA', 'BXE', 'BYT', 'BZC', 'DKWC', 'CVMD', 'CZAA', 'DCBD', 'DCUE', 'DHVA', 'DHXA', 'DHXC', 'DMFA', 'DNEA', 'DDVB', 'DDVE', 'DDVF', 'DESA', 'DEWA', 'DEZD', 'DEZF', 'DFBA', 'DKMB', 'DKNA', 'DKYA', 'DLGA', 'DLHA', 'DLHB', 'DLZA', 'EBCA', 'DLHC', 'DECA', 'AUK', 'BAT', 'BDW', 'BDX', 'BKH', 'BLB', 'BMK', 'BNA', 'BPJ', 'BPP', 'BRE', 'BRF', 'BSG', 'BVG', 'BXA', 'BYK', 'BYU', 'BGB', 'BHF', 'BKN', 'BPG', 'BPW', 'BRB', 'BRD', 'BUL', 'BVA', 'BVF', 'BWE', 'BWT', 'CAJA', 'CANA', 'CCEA', 'BKB', 'BNS', 'BRC', 'BAR', 'BHK', 'BTR', 'BUG', 'BUN', 'CASA', 'CASB', 'CCGA', 'CCMA', 'EBDA', 'DJPB', 'CWLA', 'CZDA', 'DJSA', 'DKVB', 'DKXA', 'DPLA', 'DPMA', 'CSSA', 'DJRA', 'EASA', 'EASB', 'EATA', 'EAUA', 'EAVA', 'EAWA', 'CZPC', 'DADA', 'DKJA', 'DKLA', 'DKRF', 'DKZC', 'DPCA', 'DHUB', 'DFYA', 'DGEA', 'DHFA', 'DLAA', 'DNFB', 'DSRB', 'DSUD', 'DUPA', 'EANA', 'CZPA', 'DFGA', 'DFHA', 'DHHA', 'DKTA', 'DKTC', 'CANC', 'CAND', 'CDYA', 'CDYB', 'CDYC', 'CBBB', 'CCTA', 'CCZA', 'CDAA', 'CDLA', 'CDLB', 'CDMA', 'BUJ', 'BYH', 'CMPA', 'CNDA', 'CABD', 'CAEB', 'CAGA', 'CAGB', 'CAHA', 'CAHB', 'CAKA', 'CALA', 'CAMA', 'CAMB', 'CAPA', 'CAUA', 'CCWA', 'CCWB', 'CDHB', 'CDNB', 'CDNC', 'CGKA', 'CGKB', 'CMEA', 'CJBA', 'CABA', 'CABB', 'CAEA', 'CAGC', 'CCBA', 'CCLA', 'CDHA', 'CFKA', 'CJCB', 'BZB', 'CAWB', 'CAXC', 'BUH', 'CAYC', 'CBAA', 'CBAB', 'CBZB', 'CFFA', 'CFFB', 'CAYB', 'CBFA', 'CCSA', 'CDLC', 'CFGB', 'CLJA', 'CMSA', 'CMXA', 'CALB', 'CDNA', 'CGLA', 'CGLB', 'CHJA', 'CJCA', 'CMGA', 'CANB', 'CCAA', 'CCDA', 'CADA', 'CCUA', 'CDZA', 'CHMA', 'CHMB', 'CUHA', 'CUJA', 'CDUC', 'CGLC', 'CGLD', 'CJCD', 'CKVB', 'CKVC', 'CNCD', 'CNHA', 'CNHC', 'CPMA', 'CPMB', 'CSUA', 'CSUB', 'CATA', 'CCFA', 'CCFC', 'CJGA', 'CJGC', 'CJMA', 'CJTB', 'CJTC', 'CJWB', 'CJWC', 'CJWE', 'CLZB', 'CNAA', 'CNRB', 'CRCA', 'CTWA', 'CTWB', 'CEPA', 'CEPB', 'CUHB', 'CFSA', 'CDRA', 'CDSB', 'CDTA', 'CDTB', 'CEJA', 'CEUA', 'CGTA', 'CGWA', 'CGWD', 'CGXA', 'CGXC', 'CLAB', 'CMDA', 'CMHA', 'CPAA', 'CPNA', 'CTUB', 'CESA', 'CETA', 'CAVG', 'CAXA', 'CBZA', 'CDLH', 'CFHB', 'CFHD', 'CNVA', 'CPTA', 'CTHG', 'CDUD', 'CEUC', 'CGQB', 'CGWB', 'CGXB', 'CHVA', 'CLAA', 'CLXB', 'CNYA', 'CPNB', 'CTTA', 'CTUA', 'CAED', 'CGLE', 'CMGB', 'CGWC', 'CJEB', 'CJED', 'CJEE', 'CKVD', 'CMFA', 'CMFB', 'CMUA', 'CNCE', 'CREC', 'CRED', 'CTDA', 'CCZC', 'CFGC', 'CFGD', 'CHPB', 'CLLB', 'CPSA', 'CJCC', 'CRTD', 'CRTE', 'CRTF', 'CVUA', 'CZVA', 'CZVC', 'CZVF', 'CLXA', 'CNYB', 'CHHB', 'CJSA', 'CJSB', 'CJXB', 'CJXC', 'CJXF', 'CJZA', 'CLHA', 'CMBA', 'CNSB', 'CNTC', 'CPWA', 'CRBC', 'CRBD', 'CRFA', 'CRFC', 'CRKB', 'CRLB', 'CRLC', 'CRUA', 'CUKB', 'CUNA', 'CXSA', 'CXSB', 'CXUA', 'CXXB', 'CYFB', 'CYVB', 'CZCA', 'CZEA', 'DBGA', 'DBKA', 'DEJA', 'CNCB', 'CTBA', 'CTBC', 'CTUC', 'CTUD', 'CTVA', 'CTXA', 'CVUB', 'CVUC', 'DEHA', 'CTPA', 'CTPB', 'CTYA', 'CRDB', 'CWUB', 'CWUC', 'CGMA', 'CRHA', 'CTSA', 'CZGA', 'CZGB', 'CDTC', 'CREA', 'CREG', 'CTBB', 'CTBD', 'CTEC', 'CTFA', 'CTGA', 'CTGF', 'CTNA', 'CVBA', 'CYPA', 'DDTA', 'CZCC', 'CUFA', 'CREH', 'CTCB', 'CTCC', 'CTGE', 'CVPA', 'CYGA', 'CYNB', 'CYPB', 'CZVB', 'CZVD', 'CZVE', 'CHZB', 'CHZE', 'CUSB', 'CWZA', 'CXMA', 'CZDD', 'DAJB', 'DKLD', 'CSUD', 'CSUE', 'CZJA', 'DDCA', 'DDCB', 'DDDA', 'CHZJ', 'CZPB', 'DDYA', 'DGTE', 'DKZA', 'DNUE', 'DBFB', 'DKVA', 'CVXB', 'CWWB', 'CXYA', 'CZSA', 'CZSE', 'DDVC', 'EALB', 'CSWB', 'CVKB', 'CWGD', 'CYMC', 'CYRB', 'DCPC', 'DDWA', 'DDWB', 'DEMA', 'DEMB', 'DETA', 'DEUA', 'DEZB', 'DEZE', 'DLVA', 'DMSA', 'DMSB', 'DMTA', 'DPAA', 'CRTC', 'CVKC', 'CVLA', 'CYRC', 'CZHA', 'DBPA', 'DCPE', 'DETB', 'DEUB', 'DEUC', 'DEWB', 'DLVB', 'DRXA', 'CHHC', 'CJXG', 'DKTB', 'DKZB', 'DLRA', 'DNUF', 'DAZA', 'DNWA', 'CULB', 'CULC', 'CUVB', 'CUVC', 'CUVD', 'CUWA', 'CYLA', 'CZDB', 'DBBA', 'DFTA', 'DFTB', 'DFTC', 'DFUA', 'CUEA', 'CVJA', 'CVZA', 'CZAC', 'CZZA', 'CZZB', 'DCBE', 'DHXB', 'DMFB', 'DMVA', 'EALA', 'CSPA', 'CSPB', 'DKAA', 'DKAB', 'DKAC', 'DMWA', 'CPEA', 'CTDB', 'CYYA', 'CHZD', 'CJXD', 'CZRA', 'DCYA', 'DGCA', 'DJGA', 'DJHA', 'DJHB', 'DJJA', 'EAHA', 'DBRA', 'DBSA', 'DAXB', 'DAXC', 'DAYB', 'DJYA', 'DMGA', 'DNTA', 'DTPA', 'DHDA', 'XV', 'XW', 'XY', 'XZ', 'ZD', 'ZE', 'WD', 'ATQ', 'ATW', 'AWM', 'AMU', 'AWP', 'BEL', 'BGN', 'AKB', 'AUX', 'ART', 'AWN', 'BBD', 'BEA', 'BGK', 'BNK', 'CBRA', 'CBEA']

    # 处理audi_copy表中的label字段
    label = a.label
    label = re.findall(r'[\u4e00-\u9fa5]', label)
    label = ''.join(label)

    parts = CarsPartNumNew.select().filter(CarsPartNumNew.pid == a.pid)
    for part in parts:
        cid = part.cid
        audi_structure = CarsStructureCollection.get_or_none(CarsStructureCollection.cid == cid, CarsStructureCollection.mid == a.mid)
        if audi_structure:
            structure_description = audi_structure.description or ''
            structure_sa_codes = audi_structure.sa_code or '{}'
            structure_sa_codes = ujson.loads(structure_sa_codes)
            for key, value in structure_sa_codes.items():
                if value:
                    sa_codes = sa_codes + value

            structure_model = audi_structure.model
            structure_model = structure_model.replace('<br/>', '')

            # 筛选出structure表model字段中的缸数和排量信息
            structure_out = remove_tt(structure_model)
            # 筛选出structure表description字段中的缸数和排量信息
            structure_description_out = remove_tt(structure_description)

            if structure_description:
                for ind, key in enumerate(s):
                    if key in structure_description_out:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        cylinder.append(key)

                for key in pl:
                    if key in structure_description:
                        vb_desc.append(key)
            if structure_model:
                for key in bsx:
                    if key in structure_out:
                        key = key.replace('三', '3')
                        key = key.replace('四', '4')
                        key = key.replace('五', '5')
                        key = key.replace('六', '6')
                        key = key.replace('七', '7')
                        key = key.replace('八', '8')
                        key = key.replace('档', '挡')
                        if key == '5挡':
                            key = '5挡手动'
                            if '5挡-5挡手动' not in replace:
                                replace.append('5挡-5挡手动')
                        if key == '6挡':
                            key = '6挡手动'
                            if '6挡-6挡手动' not in replace:
                                replace.append('6挡-6挡手动')
                        if key == '6挡自动':
                            key = '6挡AMT'
                            if '6挡自动-6挡AMT' not in replace:
                                replace.append('6挡自动-6挡AMT')
                        if key == '7挡自动':
                            key = '7挡双离合'
                            if '7挡自动-7挡双离合' not in replace:
                                replace.append('7挡自动-7挡双离合')
                        if key == '无级自动':
                            key = 'CVT无级变速'
                            if '无级自动-CVT无级变速' not in replace:
                                replace.append('无级自动-CVT无级变速')
                        transmission.append(key)

                for key in qd:
                    if key in structure_out:
                        key = key.replace('前驱动', '前置前驱')
                        key = key.replace('前部驱动', '前置前驱')
                        key = key.replace('四轮驱动', '全轮驱动')
                        drive.append(key)

                for ind, key in enumerate(s):
                    if key in structure_out:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        cylinder.append(key)
                for key in pl:
                    if key in structure_model:
                        vb_desc.append(key)
                for key in fdjdm:
                    if key in structure_model:
                        vb.append(key)
                if 'PR:' in structure_model or 'PR-' in structure_model or 'pr:' in structure_model or 'pr-' in structure_model:
                    # 处理structure_model中的PR
                    structure_pr = re.sub('[\u4e00-\u9fa5]', '', structure_model)
                    structure_pr = structure_pr.replace('PR:', '')
                    structure_pr = structure_pr.replace('PR-', '')
                    structure_pr = structure_pr.replace('+', ',')
                    structure_pr = structure_pr.replace('/', ',')
                    structure_pr = structure_pr.replace(':', ',')
                    structure_pr = structure_pr.replace('_', ',')
                    structure_pr = structure_pr.split(',')
                    for i in structure_pr:
                        sa_codes.append(i)
            # else:
            #     # 处理structure表发动机代码对应的排量信息
            #     structure_pr = structure_model.replace('+', ',')
            #     structure_pr = structure_pr.replace(':', ',')
            #     structure_pr = structure_pr.replace('_', ',')
            #     structure_pr = structure_pr.split(',')
            #     for i in structure_pr:
            #         vb.append(i)

        part_sa_codes = part.sa_code or '{}'

        part_sa_codes = ujson.loads(part_sa_codes)

        part_remark = part.remark or ''
        # 处理audi part表和structure表的sa_code

        for key, value in part_sa_codes.items():
            if value:
                sa_codes = sa_codes+value
        # print(sa_codes)

        #处理structure表和part表的model字段

        part_model = part.model
        part_model = part_model.replace('<br/>', '')

        # 筛选出structure表model字段中的缸数和排量信息
        part_out = remove_tt(part_model)
        part_remark_out = remove_tt(part_remark)

        if part_remark:
            for ind, key in enumerate(s):
                if key in part_remark_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)

            for key in pl:
                if key in part_remark:
                    vb_desc.append(key)
        if part_model:
            for key in bsx:
                if key in part_out:
                    key = key.replace('三', '3')
                    key = key.replace('四', '4')
                    key = key.replace('五', '5')
                    key = key.replace('六', '6')
                    key = key.replace('七', '7')
                    key = key.replace('八', '8')
                    key = key.replace('档', '挡')
                    if key == '5挡':
                        key = '5挡手动'
                        if '5挡-5挡手动' not in replace:
                            replace.append('5挡-5挡手动')
                    if key == '6挡':
                        key = '6挡手动'
                        if '6挡-6挡手动' not in replace:
                            replace.append('6挡-6挡手动')
                    if key == '6挡自动':
                        key = '6挡AMT'
                        if '6挡自动-6挡AMT' not in replace:
                            replace.append('6挡自动-6挡AMT')
                    if key == '7挡自动':
                        key = '7挡双离合'
                        if '7挡自动-7挡双离合' not in replace:
                            replace.append('7挡自动-7挡双离合')
                    if key == '无级自动':
                        key = 'CVT无级变速'
                        if '无级自动-CVT无级变速' not in replace:
                            replace.append('无级自动-CVT无级变速')
                    transmission.append(key)
            for key in qd:
                if key in part_out:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            for ind, key in enumerate(s):
                if key in part_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)
            for key in pl:
                if key in part_model:
                    vb_desc.append(key)
            for key in fdjdm:
                if key in part_model:
                    vb.append(key)
            if 'PR:' in part_model or 'PR-' in part_model or 'pr:' in part_model or 'pr-' in part_model:
                # 处理part_model中的PR
                part_pr = re.sub('[\u4e00-\u9fa5]', '', part_model)
                part_pr = part_pr.replace('PR:', '')
                part_pr = part_pr.replace('PR-', '')
                part_pr = part_pr.replace('+', ',')
                part_pr = part_pr.replace('/', ',')
                part_pr = part_pr.replace(':', ',')
                part_pr = part_pr.replace('_', ',')
                part_pr = part_pr.split(',')
                for j in part_pr:
                    sa_codes.append(j)
        # else:
        #     # 处理part表发动机代码对应的排量信息
        #     part_pr = part_model.replace('+', ',')
        #     part_pr = part_pr.replace('/', ',')
        #     part_pr = part_pr.replace(':', ',')
        #     part_pr = part_pr.replace('_', ',')
        #     part_pr = part_pr.split(',')
        #     for i in part_pr:
        #         vb.append(i)

        sa_codes = set(sa_codes)
        sa_codes = list(sa_codes)

        vb = set(vb)
        vb = list(vb)
        # print(vb)



        # 筛选sa_code对应的描述信息
        for i in sa_codes:
            contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh')
            if contrast and contrast.desc:
                if contrast.desc not in model_desc:
                    model_desc.append(contrast.desc)


        # 筛选model中发动机代码对应的排量信息
        for i in vb:
            # print(Vb.aa.zfill(3))
            # break
            if cid.isdigit():
                cid = str(int(cid))
            vb_AF = Vb.get_or_none(Vb.aa == cid, Vb.ab.contains(i))
            if vb_AF and vb_AF.af:
                # print(int(vb_AF.af)/100)
                # break
                vb_desc.append(str(int(vb_AF.af)/100))

        for i in model_desc:
            # 处理sa_code中对应的发动机配置描述信息
            if '发动机' in i:
                # 筛选出sa表desc中的排量信息
                for key in pl:
                    if key in i:
                        vb_desc.append(key)
                for key in s:
                    if key in i:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        if key not in cylinder:
                            cylinder.append(key)

            # 处理变速器配置
            if ('变速箱' in i or '变速器' in i) and ('档' in i or '无级' in i):
                i = i.replace('-', '')
                i = i.replace(' ', '')
                i = i.replace('变速箱', '')
                i = i.replace('变速器', '')
                i = i.replace('全轮驱动', '')
                i = i.replace('用于', '')
                i = i.replace('用', '')
                i = i.replace('的', '')
                i = i.replace('三', '3')
                i = i.replace('四', '4')
                i = i.replace('五', '5')
                i = i.replace('六', '6')
                i = i.replace('七', '7')
                i = i.replace('八', '8')
                i = i.replace('档', '挡')
                i = i.replace('（最低与最高挡传动比比值不同）', '')
                if i == '5挡':
                    i = '5挡手动'
                    if '5挡-5挡手动' not in replace:
                        replace.append('5挡-5挡手动')
                if i == '6挡':
                    i = '6挡手动'
                    if '6挡-6挡手动' not in replace:
                        replace.append('6挡-6挡手动')
                if i == '6挡自动':
                    i = '6挡AMT'
                    if '6挡自动-6挡AMT' not in replace:
                        replace.append('6挡自动-6挡AMT')
                if i == '7挡自动':
                    i = '7挡双离合'
                    if '7挡自动-7挡双离合' not in replace:
                        replace.append('7挡自动-7挡双离合')
                if i == '无级自动':
                    i = 'CVT无级变速'
                    if '无级自动-CVT无级变速' not in replace:
                        replace.append('无级自动-CVT无级变速')
                transmission.append(i)

            # 处理驱动配置
            for key in qd:
                if key in i:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            # 处理差速器配置
            if '差速' in i or '减速' in i:
                diff_mech.append(i)
    print(vb)
    vb_desc = set(vb_desc)
    vb_desc = list(vb_desc)
    vb_desc = ','.join(vb_desc)

    cylinder = set(cylinder)
    cylinder = list(cylinder)
    cylinder = ','.join(cylinder)

    transmission = set(transmission)
    transmission = list(transmission)
    transmission = ','.join(transmission)

    drive = set(drive)
    drive = list(drive)
    drive = ','.join(drive)

    diff_mech = set(diff_mech)
    diff_mech = list(diff_mech)
    diff_mech = ','.join(diff_mech)

    model_desc = ','.join(model_desc)
    vb = ','.join(vb)
    replace = ','.join(replace)

    item.append({'pid': a.pid, 'standard_label': a.standard_label, 'label': label, 'desc': model_desc, 'engine_code': vb,
                 'cylinder': cylinder, 'transmission': transmission, 'replace': replace, 'drive': drive,
                 '差速器': diff_mech, 'displacement': vb_desc})
df = pd.DataFrame(item)
df.to_csv('/Users/liuchong/standartools/scripts/audi/export/audi_all01.csv', mode='a', index=False)


